using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class RayCastController : MonoBehaviour
{
    private readonly RaycastHit[] hits = new RaycastHit[10];
    public Vector2 pos;
    public new Camera camera;
    public List<string> hitNames;

    private void Awake()
    {
        camera = GetComponent<Camera>();
    }

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            hitNames.Clear();
            pos = Input.mousePosition;
            var ray = camera.ScreenPointToRay(pos);
            var count = Physics.RaycastNonAlloc(ray, hits);
            if (count > 0)
            {
                foreach (var hit in hits)
                {
                    if (count <= 0) break;
                    if (hit.transform != null)
                    {
                        hitNames.Add(hit.transform.name);
                        count--;
                    }
                }
            }
        }
    }
}
